package com.tedu.project5.mapper;

import com.tedu.project5.model.DdPermission;
import com.tedu.project5.model.DdRole;
import com.tedu.project5.model.DdUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* <p>
    *  Mapper 接口
    * </p>
*
* @author mhg
* @since 2022-06-13
*/
@Mapper
public interface DdUserMapper extends BaseMapper<DdUser> {

    /*通过账号查询信息*/
    @Select("select * from dd_user where username = #{username}")
    DdUser findUserByUserName(String username);

    /**
     * 通过用户id查询权限
     * @param id
     * @return
     */
    @Select("SELECT p.id, p.permission_name\n" +
            "FROM dd_user u\n" +
            "LEFT JOIN dd_user_role ur ON u.id=ur.user_id\n" +
            "LEFT JOIN dd_role r       ON r.id=ur.role_id\n" +
            "LEFT JOIN dd_role_permission rp ON r.id=rp.role_id\n" +
            "LEFT JOIN dd_permission p ON p.id=rp.permission_id\n" +
            "WHERE u.id=#{id}")
    List<DdPermission> findUserPermissionsById(Integer id);


    /**
     * 根据用户id查询用户角色
     * @param id
     * @return
     */
    @Select("SELECT r.id, r.role_name \n" +
            "FROM dd_user u\n" +
            "LEFT JOIN dd_user_role ur ON u.id=ur.user_id\n" +
            "LEFT JOIN dd_role r       ON r.id=ur.role_id\n" +
            "WHERE u.id=#{id}")
    List<DdRole> findUserRolesById(Integer id);


}
